﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors;
using DevTeam;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace ConfExpert.Forms.Sistem
{
    public partial class Templates : XForm
    {
        string kategori;
        public Templates(string kategori)
        {
            InitializeComponent();
            this.kategori = kategori;
            this.Text += "- " + kategori;
        }

        private void Templates_Load(object sender, EventArgs e)
        {
            gridView1.Init("GetTemplates", kategori, 0);
            gridView1.AddSummary();
            gridView1.BestFitColumns();

            btnYeni.ItemClick += btnYeni_ItemClick;
            btnKaydet.ItemClick += btnKaydet_ItemClick;

            GridManager gm = GridManager.GetManager(gridView1);
            gm.GridMenu.AddMenu("Aç", Ac);
            gm.GridMenu.AddMenu("Değiştir", Degistir);
            if (ytk(17))
                gm.GridMenu.AddMenu("Sil", Sil);

        }
        string FileToSend;

        void Ac(object sender, EventArgs e)
        {
            int TemplatesId = gridView1.GetDataRow(gridView1.FocusedRowHandle)["TemplatesId"].ToInt();
            if (TemplatesId > 0)
            {
                DataTable t = Session.DataManager.ExecuteDataTable("GetTemplates", kategori, TemplatesId);
                FileToSend = ByteArrayToFile3(t.Rows[0]["DosyaAdi"].ToString(), t.Rows[0]["BLOB"] as byte[]);

                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = FileToSend;
                process.Start();
            }
        }

        void Rfrsh()
        {
            gridView1.RefreshDataSource("GetTemplates", kategori, 0);
            gridView1.BestFitColumns();
        }

        void Degistir(object sender, EventArgs e)
        {
            int TemplatesId = gridView1.GetDataRow(gridView1.FocusedRowHandle)["TemplatesId"].ToInt();
            if (TemplatesId > 0)
            {
                using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                        if (openFileDialog1.OpenFile() != null)
                        {
                            string FileName = Path.GetFileName(openFileDialog1.FileName),
                                Extension = Path.GetExtension(openFileDialog1.FileName);
                            //md5 = MD5.GetHashFromFile(openFileDialog1.FileName);
                            byte[] bArr = FileManager.Read(openFileDialog1.FileName);
                            ExecuteNonQuery("UpdTemplate", kategori, TemplatesId, openFileDialog1.SafeFileName, bArr, WhoAmI);
                            Notify("Dosya güncellendi.");
                            Rfrsh();
                        }
            }
        }

        void Sil(object sender, EventArgs e)
        {
            int TemplatesId = gridView1.GetDataRow(gridView1.FocusedRowHandle)["TemplatesId"].ToInt();
            if (TemplatesId > 0)
                if (XtraMessageBox.Show("Bu dosyayı silmek istediğinizden emin misiniz?", "Teyid", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    ExecuteNonQuery("DelTemplate", kategori, TemplatesId);
                    Notify("Dosya silindi.");
                    Rfrsh();
                }
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.Update("TemplatesId,Aciklama", "Templates" + kategori);
        }

        void btnYeni_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    if (openFileDialog1.OpenFile() != null)
                    {
                        string FileName = Path.GetFileName(openFileDialog1.FileName),
                            Extension = Path.GetExtension(openFileDialog1.FileName);
                        //md5 = MD5.GetHashFromFile(openFileDialog1.FileName);
                        byte[] bArr = FileManager.Read(openFileDialog1.FileName);
                        ExecuteNonQuery("UpdTemplate", kategori, 0, openFileDialog1.SafeFileName, bArr, WhoAmI);
                        Notify("Dosya eklendi.");
                        Rfrsh();
                    }

        }

        public string ByteArrayToFile3(string filePath, byte[] content)
        {
            string FullPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache) + "\\" + filePath;
            using (FileStream fs = new FileStream(FullPath, FileMode.Create, FileAccess.Write))
            using (BinaryWriter bw = new BinaryWriter(fs))
            {
                bw.Write(content);
                bw.Flush();
            }


            return FullPath;
        }
    }
}
